<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
class forum extends MY_Controller {
	function __construct(){
		parent::__construct();
		$this->load->model("forum_model","forumModel");
	}

	function index(){
		PageUtil::addVar("title","กระดานสนทนา - ".$this->config->item("sitename","site"));
		$userdata = $this->session->userdata("user_data");
		$this->_assign("userdata",$userdata);
		$this->load->model("forumcategory_model","forumcategoryModel");
		$cats = $this->forumcategoryModel->get(array("order"=>"sort ASC"));
		$categories = array();
 		if(!empty($cats)){
			foreach ($cats as $key => $value) {

				$categories['items'][$value['id']] = $value;
				$categories['parents'][$value['parent_id']][] = $value['id'];
		 	}
	  }
		//Assign
		$this->_assign('categories',$categories);
		$this->_fetch("index");
	}

	function create(){
		if(!$this->ion_auth->logged_in()){
			redirect(base_url("user/login"),"refresh");
		}
		$this->load->model("forumcategory_model","forumcategoryModel");
		$cats = $this->forumcategoryModel->get(array("order"=>"sort ASC"));
		$categories = array();
 		if(!empty($cats)){
			foreach ($cats as $key => $value) {
		    $categories['items'][$value['id']] = $value;
		    $categories['parents'][$value['parent_id']][] = $value['id'];
		 	}
	  }

		//Assign
		$this->_assign('categories',$categories);
		$this->_fetch("form");
	}

	function add(){
		if(!$this->ion_auth->logged_in()){
			redirect(base_url("user/login"),"refresh");
		}
		$_post = $this->input->post();
		if(!empty($_post["submit"])){
			$userdata = $this->ion_auth->user_array();
			if(empty($_post['id'])){
				$_post['usr_id'] = $userdata['id'];
			}
			if(empty($_post["title"])){
				show_error("กรุณาใส่หัวข้อ");
			}

			if(empty($_post["id"])){
				$_post["url"] = Util::url_title(trim($_post["title"]));
				unset($where);
				$where["where"]["title"] = trim($_post["title"]);
				$isThere = $this->forumModel->get($where);

				if(!empty($isThere)){
					$_post["url"] = Util::url_title(trim($_post["title"]))."-".count($isThere);
				}
				$_post["usr_id"] = $userdata['id'];
			}
			$_post['status'] = 1;

			//Set date for update post
			$_post["update_date"] = date('Y-m-d H:i:s');
			$post_id = $this->forumModel->add($_post);

			// make directory && check
			//check
			$path = "resource/".$post_id ;
			if(!file_exists($path)){
				mkdir('resource/'.$post_id, 0777, TRUE);
			}

			//load library image Manipulation for resize
			$this->load->library('image_lib');
			//load library upload
			$this->load->library('upload');
			//config
 			$config['upload_path'] = $path ;
			$config['allowed_types'] = 'gif|jpg|png';
			$config['max_size']	= '102400';
			$config['max_width']  = '10240';
			$config['max_height']  = '7680';
			$config['encrypt_name'] = 'true';
			$this->upload->initialize($config);
			$totlecount = count($_FILES['filename']['name']);

			$groups = $_FILES['filename']['name'];


			foreach ($groups as $key => $group) {
					if(!empty($_FILES['filename']['name'][$key])){
						$_FILES['userfile']['name']    = $_FILES['filename']['name'][$key];
						$_FILES['userfile']['type']    = $_FILES['filename']['type'][$key];
						$_FILES['userfile']['tmp_name'] = $_FILES['filename']['tmp_name'][$key];
	 					$_FILES['userfile']['error']       = $_FILES['filename']['error'][$key];
	 					$_FILES['userfile']['size']    = $_FILES['filename']['size'][$key];

						if (!$this->upload->do_upload()){
							log_message('error', $this->upload->display_errors());
						}else{

							$picturesData[$key] = $this->upload->data();

							$file_name = $picturesData[$key]["file_name"];
							//explode
							$image_name = explode(".", $file_name);

							$picturesData[$key]["large"] = $path."/".$image_name[0]."_mainSize.".$image_name[1];
							$picturesData[$key]["medium"] = $path."/".$image_name[0]."_mSize.".$image_name[1];
							$picturesData[$key]["small"] = $path."/".$image_name[0]."_sSize.".$image_name[1];

							//mainImage
							$config['image_library'] = 'GD2';
							// รูปที่เอามาใช้ในการ resize
							$config['source_image'] = $picturesData[$key]["full_path"];
							$config['maintain_ratio'] = TRUE;
							$config['new_image'] = $picturesData[$key]["large"];
							$config['width'] = 1024;
							$config['height'] = 680;
							$dim = (intval($picturesData[$key]["image_width"]) / intval($picturesData[$key]["image_height"])) - ($config['width'] / $config['height']);
							$config['master_dim'] = ($dim > 0) ? "height" : "width";
							$this->image_lib->clear();
							$this->image_lib->initialize($config);
							$this->image_lib->resize();

							//add water mark
							unset($config);
							$config['image_library'] = 'GD2';
							$config['source_image'] = $picturesData[$key]["large"];
							$config['wm_type'] = 'overlay';
							$config['wm_opacity'] = 100;
							$config['wm_overlay_path'] = "themes/".$this->config->item("theme_name").'/images/watermark.png';
							$config['wm_vrt_alignment'] = 'top';
							$config['wm_hor_alignment'] = 'right';
							$this->image_lib->clear();
							$this->image_lib->initialize($config);
							if(!$this->image_lib->watermark()){
								echo $this->image_lib->display_errors();exit;
							}
							unset($config);

							//mediumImage Resizing
							$config3['image_library'] = 'GD2';
							$config3['source_image'] = $picturesData[$key]["full_path"];
							$config3['new_image'] = $picturesData[$key]["medium"];
							$config3['maintain_ratio'] = TRUE;
							$config3['width'] = 426;
							$config3['height'] = 328;
							$dim = (intval($picturesData[$key]["image_width"]) / intval($picturesData[$key]["image_height"])) - ($config3['width'] / $config3['height']);
							$config3['master_dim'] = ($dim > 0) ? "height" : "width";
							$this->image_lib->clear();
							$this->image_lib->initialize($config3);
							$this->image_lib->resize();

							unset($config3);
							//mediumImage Cropping
							$config3['image_library'] = 'GD2';
							$config3['source_image'] = $picturesData[$key]["medium"];
							$config3['overwrite'] = TRUE;
							$config3['quality'] = "100%";
							$config3['width'] = 426;
							$config3['height'] = 328;
							$config3['maintain_ratio'] = FALSE;
							$config3['x_axis'] = 0;
							$config3['y_axis'] = 0;
							$this->image_lib->clear();
							$this->image_lib->initialize($config3);
							if (!$this->image_lib->crop()){
							}
							unset($config3);

							//add water mark
							unset($config);
							$config['image_library'] = 'GD2';
							$config['source_image'] = $picturesData[$key]["medium"];
							$config['wm_type'] = 'overlay';
							$config['wm_opacity'] = 100;
							$config['wm_overlay_path'] = "themes/".$this->config->item("theme_name").'/images/watermark-m.png';
							$config['wm_vrt_alignment'] = 'top';
							$config['wm_hor_alignment'] = 'right';
							$this->image_lib->clear();
							$this->image_lib->initialize($config);
							$this->image_lib->watermark();
							unset($config);

							//resize smallImage
							$config2['image_library'] = 'GD2';
							$config2['source_image'] = $picturesData[$key]["full_path"];
							$config2['new_image'] = $picturesData[$key]["small"];
							$config2['maintain_ratio'] = TRUE;
							$config2['width'] = 167;
							$config2['height'] = 90;
							$dim = (intval($picturesData[$key]["image_width"]) / intval($picturesData[$key]["image_height"])) - ($config2['width'] / $config2['height']);
							$config2['master_dim'] = ($dim > 0) ? "height" : "width";
							$this->image_lib->clear();
							$this->image_lib->initialize($config2);
							if(!$this->image_lib->resize()){
							}

							unset($config2);
							//mediumImage Cropping
							$config2['image_library'] = 'GD2';
							$config2['source_image'] = $picturesData[$key]["small"];
							$config2['overwrite'] = TRUE;
							$config2['quality'] = "100%";
							$config2['width'] = 167;
							$config2['height'] = 90;
							$config2['maintain_ratio'] = FALSE;
							$config2['x_axis'] = 0;
							$config2['y_axis'] = 0;
							$this->image_lib->clear();
							$this->image_lib->initialize($config2);
							if (!$this->image_lib->crop()){
							}
							unset($config2);

							//add water mark
							unset($config);
							$config['image_library'] = 'GD2';
							$config['source_image'] = $picturesData[$key]["full_path"];
							$config['wm_type'] = 'overlay';
							$config['wm_opacity'] = 100;
							$config['wm_overlay_path'] = "themes/".$this->config->item("theme_name").'/images/watermark.png';
							$config['wm_vrt_alignment'] = 'top';
							$config['wm_hor_alignment'] = 'right';
							$this->image_lib->clear();
							$this->image_lib->initialize($config);
							if(!$this->image_lib->watermark()){
								echo $this->image_lib->display_errors();exit;
							}
							unset($config);
						}
					}
			}

			if(!empty($picturesData)){
				$first_image = false;
				foreach ($picturesData as $key4 => $value) {
			 		$this->load->model("images_model","imagesModel");
			 		$image['pos_id'] = $post_id;
			 		$image['filesize'] = $value["file_size"];
			 		$image['type'] = $value["image_type"];
			 		$image['filename'] = $value["file_name"];
			 		$image['filepath'] = $path."/".$value["file_name"];
			 		$image['large'] = $value["large"];
			 		$image['medium'] = $value["medium"];
			 		$image['small'] = $value["small"];
			 		if(!$first_image){
			 			$image['firstimage'] = 1;
			 			$first_image = TRUE;
			 		}else{
			 			$image['firstimage'] = 0;
			 		}
			 		$this->imagesModel->add($image);
				 }
			}
			$this->session->set_flashdata('success', 'เพิ่มกระทู้เรียบร้อย');
		}else{
			$this->session->set_flashdata('error', 'มีข้อผิดผลาดเกิดขึ้นชณะทำการเพิ่มกระทู้ อาจะทำให้การเพิ่มกระทู้ไม่สมบูรณ์');
			log_message('error', 'Error occurred on '.__LINE__.' at '.__FILE__);
		}
		redirect(base_url("forum/view/".$_post["url"]));
	}

	function view($url = NULL){
		if(empty($url)){
			redirect("forum","refresh");
		}
		$where['where']['url'] = urldecode(trim($url));
		$data = $this->forumModel->get($where);
		if(empty($data)){
			show_404();
		}
		PageUtil::addVar("title",$data[0]["title"]." - ".$this->config->item("sitename","site"));

		//update view
		$dataUpdateView["id"] = $data[0]["id"];
		$dataUpdateView["view"] = ($data[0]["view"]+1);
		$updateViewResult = $this->forumModel->add($dataUpdateView);
		if(!$updateViewResult) {
			log_message('error', 'can\'t update view in post at '.__FILE__.' line ['.__LINE__.']');
		}
		//end update view
		if(!empty($data[0]["images"])){
			foreach($data[0]["images"] as $image){
				if($image["firstimage"]){
					$this->_assign('first_images',$image);
				}
				$images[] = $image;
			}
			$this->_assign('images',$images);
		}
		$userdata = $this->ion_auth->user_array();
		$this->_assign("userdata",$userdata);
		$this->_assign("post",$data[0]);

		$categories = array_reverse(Util::getForumCategoryInherit($data[0]["cat_id"]));
		$breadcrumb = "<a href=\"".base_url()."\">หน้าแรก</a> &raquo; <a href=\"".base_url("forum")."\">กระดานสนทนา</a>";
		foreach ($categories as $key => $value) {
			$urlCat[] = $value["url"];
			$keywords[] = $value["name"];
			$breadcrumb .= " &raquo; <a href=\"".base_url("forum/category/".$value["url"])."\">".$value["name"]."</a>";
		}
		$this->_assign("breadcrumb",$breadcrumb." &raquo; <a href=\"".base_url("forum/view/".$data[0]["url"])."\">".$data[0]["title"]."</a>");

		//Cache comment
		//if(!$dataCom = $this->cache->get("postview/postview_".md5($url)."/comment_".md5($url))){
			$this->load->model("comment_model","commentModel");
			$whereComment['where']['pos_id'] = $data[0]["id"];
			$whereComment['where']['status'] = 1;
			$whereComment['order'] = "id ASC";
			$dataCom = $this->commentModel->get($whereComment);
			//$this->cache->write($dataCom,"postview/postview_".md5($url)."/comment_".md5($url),(3600*24));
		//}
		//End Cache comment
		$this->_assign("comments",$dataCom);
		if(empty($dataCom)){
			$this->_assign("comment_count",0);
		}else{
			$this->_assign("comment_count",count($dataCom));
		}

		//get keyword
		$configKeywordList = $this->config->item("site");
		for($i=0;(count($keywords)<10 AND !empty($configKeywordList["keyword"][$i]));$i++){
			$keywords[] = $configKeywordList["keyword"][$i];
		}
		$keywords = implode(", ", $keywords);
		$this->_assign("metaKeyword",$keywords);

		
		$this->_fetch("view");
	}

	function edit($id = NULL){
		if(empty($id)){
			redirect("forum","refresh");
		}
		if(!$this->ion_auth->logged_in()){
			redirect(base_url("user/login"),"refresh");
		}
		$userdata = $this->ion_auth->user_array();

		if(!$this->ion_auth->is_admin()){
			$where['where']['usr_id'] = $userdata['id'];
		}

		$where['where']['id'] = $id;
		$data = $this->forumModel->get($where);
		if(empty($data)){
			show_404();
		}

		$this->load->model("images_model","imagesModel");
    
    foreach ($data as $key => $value) {
      $whereImage["where"]["pos_id"] = $value["id"];
      $images = $this->imagesModel->get($whereImage);
      $data[$key]["images"] = $images;
    }
    $this->load->model("forumcategory_model","forumcategoryModel");
		$cats = $this->forumcategoryModel->get(array("order"=>"sort ASC"));
		$categories = array();
 		if(!empty($cats)){
			foreach ($cats as $key => $value) {
				$categories['items'][$value['id']] = $value;
				$categories['parents'][$value['parent_id']][] = $value['id'];
		 	}
	  }

		//Assign
		$this->_assign('categories',$categories);
		$this->_assign("userdata",$userdata);
		$this->_assign("data",$data);
		$this->_fetch("edit");
	}

	function delete($id = NULL){
		if(!$this->ion_auth->logged_in()){
			redirect(base_url("user/login"),"refresh");
		}
		$userdata = $this->session->userdata("user_data");
		if($userdata['group'] != 2){
			$where['where']['usr_id'] = $userdata['id'];
		}
		$where['where']['id'] = $id;
		$data = $this->forumModel->get($where);
		if(empty($data)){
			show_404();
		}
		$dataToChange["id"] = $id;
		$dataToChange["status"] = 0;
		$this->forumModel->add($dataToChange);

		redirect(base_url("forum"));
	}

	function comment_forum(){
		if(!$this->ion_auth->logged_in()){
			redirect(base_url("user/login"),"refresh");
		}
		$_post = $this->input->post();
		$userdata = $this->session->userdata("user_data");
		$_post['status'] = 1;
		$_post['usr_id'] = $userdata['id'];
		$this->load->model("comment_model","commentModel");
		$result = $this->commentModel->add($_post);
		redirect(base_url("forum/view/".$_post['pos_id']));
	}

	function replycomment_forum($id=""){
		if(!$this->ion_auth->logged_in()){
			redirect(base_url("user/login"),"refresh");
		}
		$this->load->model("comment_model","commentModel");
		$where['where']['id'] = $id;
		$data = $this->commentModel->get($where);
		$userdata = $this->session->userdata("user_data");
		$this->_assign("userdata",$userdata);
		$this->_assign("data",$data);
		$this->_fetch("replycomment_forum");
		
	}
	
	function editcommnt_forum($id=""){
		if(!$this->ion_auth->logged_in()){
			redirect(base_url("user/login"),"refresh");
		}
		$this->load->model("comment_model","commentModel");
		$userdata = $this->session->userdata("user_data");
		if($userdata['group'] != 2){
			$where['where']['usr_id'] = $userdata['id'];
		}
		$where['where']['id'] = $id;
		$data = $this->commentModel->get($where);
		if(empty($data)){
			show_404();
		}
		$userdata = $this->session->userdata("user_data");
		$this->_assign("userdata",$userdata);
		$this->_assign("data",$data);
		$this->_fetch("editcomment_forum");
	}

	function category($url = NULL){
		if(empty($url)){
			redirect("forum","refresh");
		}
		
		$this->load->model("forumcategory_model","forumcategoryModel");
		$this->load->model("images_model","imagesModel");
		$this->load->library('pagination');
		
		//config pagination
		$config['prev_link'] = '&laquo;';
    $config['prev_tag_open'] = '<li>';
    $config['prev_tag_close'] = '</li>';

    $config['next_link'] = '&raquo;';
    $config['next_tag_open'] = '<li>';
    $config['next_tag_close'] = '</li>';

    $config['num_tag_open'] = '<li>';
    $config['num_tag_close'] = '</li>';

    $config['cur_tag_open'] = '<li class="active"><a href="#">';
    $config['cur_tag_close'] = '</a></li>';

    $config['first_tag_open'] = '<li>';
    $config['first_tag_close'] = '</li>';

    $config['last_tag_open'] = '<li>';
    $config['last_tag_close'] = '</li>';

    //get all the URI segments for pagination and sorting
    $segment_array = $this->uri->segment_array();
    $segment_count = $this->uri->total_segments();

    $config['base_url'] = base_url('forum/category/'.$url);

    //get forumcategory and assign to view
		$whereCategory["where"]["url"] = urldecode(trim($url));
		$resultCategory = $this->forumcategoryModel->get($whereCategory);
		if(empty($resultCategory)){
			redirect("forum","refresh");
		}
		PageUtil::addVar("title","กระดานสนทนา &raquo; ".$resultCategory[0]["name"]." - ".$this->config->item("sitename","site"));
		$this->_assign("forum",$resultCategory);

		//get total rows of current category
		$whereCount["where"]["status"] = 1;
		$whereCount["where"]["cat_id"] = $resultCategory[0]["id"];
		$config['total_rows'] = $this->forumModel->count_rows($whereCount);
		$config['per_page'] = 20;

		//getting the records and limit setting
    if (ctype_digit($segment_array[$segment_count])) {
			$where['limit']['amount'] = $config['per_page'];
			if($segment_array[$segment_count] > 0){
				$where['limit']['start'] = $segment_array[$segment_count];
			}else{
				$where['limit']['start'] = 0;
			}
      $start_offset = ($segment_array[$segment_count]+1);
      $end_offer = ($segment_array[$segment_count]+$config['per_page']);
      if($end_offer > $config['total_rows']){
        $end_offer = $config['total_rows'];
      }
      array_pop($segment_array);
    } else {
      $where['limit']['amount'] = $config['per_page'];
      $where['limit']['start'] = 0;
      $start_offset = 1;
    }

    $config['uri_segment'] = count($segment_array)+1;

		$where["where"]["status"] = 1;
		$where["where"]["cat_id"] = $resultCategory[0]["id"];
		$where["order"] = "update_date DESC";
		$listpost = $this->forumModel->get($where);
		if(!empty($listpost)){
      foreach ($listpost as $key => $value) {
        $whereImage["where"]["pos_id"] = $value["id"];
        $whereImage["order"] = "id ASC";
        $images = $this->imagesModel->get($whereImage);
        $listpost[$key]["images"] = $images;
      }
    }

    if((count($listpost)+1)>$start_offset){
      $end_offer = count($listpost);
    }

		$this->pagination->initialize($config);

    $this->_assign("end_offset",$end_offer);
    $this->_assign("start_offset",$start_offset);

		$this->_assign("listpost",$listpost);
		
		$this->_fetch("forumlist");
	}

	
	function delete_image($id = NULL){
		if(!$this->ion_auth->logged_in()){
			show_404();
		}

		if(empty($id)){
			show_404();
		}

		$image_id = $id;
		$this->load->model("images_model","imagesModel");
		$where['where']['id'] = $image_id;
		$detail_image = $this->imagesModel->get($where);

		$resultPost = $this->forumModel->get($detail_image[0]["pos_id"]);
		$userdata = $this->ion_auth->user_array();

		if($resultPost[0]['usr_id'] == $userdata['id'] OR $this->ion_auth->in_group("admin"))
		{
			if(!empty($detail_image)){
				if(file_exists($detail_image[0]["filepath"])){
					@unlink($detail_image[0]["filepath"]);
				}
				if(file_exists($detail_image[0]["large"])){
					@unlink($detail_image[0]["large"]);
				}
				if(file_exists($detail_image[0]["medium"])){
					@unlink($detail_image[0]["medium"]);
				}
				if(file_exists($detail_image[0]["small"])){
					@unlink($detail_image[0]["small"]);
				}
				$this->imagesModel->delete($image_id);
			}
		}
		redirect(base_url("forum/edit/".$detail_image[0]["pos_id"]));
	}

	/***
	//Admin
	***/

	function admin_index(){
		$this->load->model("forumcategory_model","forumcategoryModel");
	 	$result = $this->forumcategoryModel->get(array("order"=>"sort ASC"));
	 	$categories = array();
	 	if(!empty($result)){
		 	foreach ($result as $key => $value) {
		    $categories['items'][$value['id']] = $value;
		    $categories['parents'][$value['parent_id']][] = $value['id'];
		 	}
	 	}
		$this->_assign('categories',$categories);
		$this->_fetch('admin_index');
	}

	function admin_addcategory(){
		$_post = $this->input->post();
		$this->load->model("forumcategory_model","forumcategoryModel");
		$this->load->model('tag_model','tagModel');
		if(empty($_post["submit"])){
			$tags = $this->tagModel->get();
			$this->_assign('tags',$tags);
			$cats = $this->forumcategoryModel->get(array("order"=>"sort ASC"));
			$categories = array();
	 		if(!empty($cats)){
				foreach ($cats as $key => $value) {
			    $categories['items'][$value['id']] = $value;
			    $categories['parents'][$value['parent_id']][] = $value['id'];

			 	}
		  }
			//Assign
			$this->_assign('categories',$categories);
			//Fetch
			$this->_fetch('admin_addcategory');
		}else{
			//Delete cache
			$this->cache->delete_folder("category");
			$this->cache->delete("menu_widget_html");

			if(!empty($_post["parent_id"])){

			}else{
				$_post["parent_id"] = 0;
			}
			if(!empty($_post["tag_name"])){
				$tag_name = explode("\n", $_post["tag_name"]);
				unset($_post["tag_name"]);
				foreach ($tag_name as $key => $value) {
					$tag_name[$key] = trim($value);
					$where["where"]["name"] = $tag_name[$key];
					$isThere = $this->tagModel->get($where);
					if(empty($isThere)){
						$dataInsert["name"] = $tag_name[$key];
						$dataInsert["url"] = Util::url_title($tag_name[$key]);
						$tag_id = $this->tagModel->add($dataInsert);
					}else{
						$tag_id = $isThere[0]["id"];
					}
					$_post["tag_id"] = $tag_id;
					$result = $this->forumcategoryModel->add($_post);
				}
			}else{
				$result = $this->forumcategoryModel->add($_post);
			}
			redirect(base_url("admin/forum"));
		}
	}

	function admin_editcategory($id = ""){
		if(empty($id)){
			show_404();
		}
		$post = $this->input->post();
		$this->load->model("forumcategory_model","forumcategoryModel");
		if(!empty($post["submit"])){
			//Submit
			$result = $this->forumcategoryModel->add($_post);
		}else{
			$this->load->model('tag_model','tagModel');
			$tags = $this->tagModel->get();
			$this->_assign('tags',$tags);

			$cats = $this->forumcategoryModel->get(array("order"=>"sort ASC"));
			$categories = array();
	 		if(!empty($cats)){
				foreach ($cats as $key => $value) {
			    $categories['items'][$value['id']] = $value;
			    $categories['parents'][$value['parent_id']][] = $value['id'];

			 	}
		  }
			//Assign
			$this->_assign('categories',$categories);
			//show data
			$result = $this->forumcategoryModel->get($id);
			$this->_assign("data",$result[0]);
			$this->_fetch("admin_editcategory");
		}
	}

	function admin_delete($id=""){
		$this->load->model("forumcategory_model","forumcategoryModel");
	 	$this->forumcategoryModel->delete($id);
	 	//Delete cache
		$this->cache->delete_folder("category");
		$this->cache->delete("menu_widget_html");

	 	redirect(base_url("admin/forum"));
	}

	function admin_orderupdate(){
		$this->load->model("forumcategory_model","forumcategoryModel");
		$_post = $this->input->post();
		foreach ($_post["sort"] as $key => $value) {
			$data["id"] = $key;
			$data["sort"] = $value;
			$this->forumcategoryModel->add($data);
		}
		//Delete cache
		$this->cache->delete_folder("category");
		$this->cache->delete("menu_widget_html");
		redirect(base_url("admin/forum"));
	}

}
